package com.example.desksginktapp.views

import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import com.example.baselibrary.bases.Constants
import com.example.baselibrary.utils.ImageLoaderUtils
import com.example.baselibrary.utils.QRCodeUtils
import com.example.baselibrary.utlis.LogUtils
import com.example.baselibrary.utlis.setOnSingleClickListener
import com.example.baselibrary.wigits.XLoadingDialogManger
import com.example.desksginktapp.R
import com.example.desksginktapp.databinding.ActivityMainBinding
import com.example.desksginktapp.viewmodels.MainViewModel
import com.example.desksginktapp.wigit.PasswordDialogFragment
import com.xuexiang.xui.utils.XToastUtils
import com.zyf.baselibrary.base.BaseViewBindingActivity

class MainActivity : BaseViewBindingActivity<ActivityMainBinding, MainViewModel>() {
    private var currentDialog: PasswordDialogFragment? = null
    override fun provideViewModel(): MainViewModel {
        return MainViewModel()
    }

    override fun initViews() {
        super.initViews()
        addTouchFeedback(binding.llMainInvitedVisitor)
        addTouchFeedback(binding.llMainVisitQuery)
        addTouchFeedback(binding.llMainSignOut)
        addTouchFeedback(binding.llMainSetting)
        binding.llMainSetting.setOnSingleClickListener {
//          showInputDialog()
            navigateTo(SettingActivity::class.java)
        }
        binding.llMainInvitedVisitor.setOnSingleClickListener {
        }

        binding.llMainVisitQuery.setOnSingleClickListener {
            //  navigateTo(FaceDetectionActivity::class.java)
        }
        binding.llMainSignOut.setOnSingleClickListener {
            //  showInputDialog()
        }
        val newUrl = mAcache?.getAsString(Constants.URL) ?: ""
        if (newUrl.isNotEmpty()) {
            ImageLoaderUtils.load(this, newUrl + "/visitorMachine/getTemporaryVisitorQrCode")
                .placeholder(R.drawable.happy)
                .error(R.drawable.cry)
                .into(binding.ivQrShow)
        }


    }

    private fun showInputDialog() {
        // 清理之前的对话框引用
        currentDialog = null

        // 创建新的对话框
        val dialog = PasswordDialogFragment().apply {
            setPasswordDialogListener(object : PasswordDialogFragment.PasswordDialogListener {
                override fun onPasswordCorrect() {
                    XToastUtils.success("验证码验证成功！")
                    navigateTo(SettingActivity::class.java)

                }

                override fun onDialogDismissed() {
                    Log.d("PasswordDialog", "对话框已关闭")
                    currentDialog = null // 清理引用
                }
            })
        }

        currentDialog = dialog

        binding.root.postDelayed({
            if (!isFinishing && !isDestroyed) {
                try {
                    dialog.show(supportFragmentManager, "PasswordDialog")
                } catch (e: Exception) {
                    Log.e("MainActivity", "显示对话框失败", e)
                    currentDialog = null
                }
            } else {
                currentDialog = null
            }
        }, 100)
    }

    override fun initData() {
        super.initData()
    }


    override fun onDestroy() {
        // 清理对话框引用
        currentDialog?.dismissAllowingStateLoss()
        currentDialog = null
        super.onDestroy()
    }

}
